﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ExSystem.ExData
{
   public class ExDataTable
    {
      /// <summary>
        /// 重新构造DataTable，让其所有列的数据都为newType指定类型(可在参数exceptetionColumns中定义排除列)，并可以对转换的数据通过iformat进行处理
      /// </summary>
        /// <param name="dt">要排序的表</param>
        /// <param name="newDataType"></param>
        /// <param name="exceptetionColumns">指定不需要重构的列名</param>
      /// <returns></returns>
        public static DataTable ToNewTypeColumn(DataTable dt,Type newDataType, string[] exceptetionColumns,ExITableCellFormat iformat)
        {

            if (dt == null) return dt;
            if (dt.Columns.Count <= 0) return dt;
            
           
            DataTable dt2 =new DataTable();
        
            DataRow rowx;
            foreach (DataColumn co in dt.Columns)
            {
                if (!ExSystem.ExString.Contains(exceptetionColumns, co.ColumnName))
                { dt2.Columns.Add(new DataColumn(co.ColumnName, newDataType)); }
                else
                {
                    dt2.Columns.Add(new DataColumn(co.ColumnName, co.DataType));
                }

            }
            foreach (DataRow row in dt.Rows)
            {
                rowx = dt2.NewRow();
                foreach (DataColumn co in dt2.Columns)
                {
                    if (iformat != null)
                    {
                        rowx[co.ColumnName] = iformat.Format(row[co.ColumnName]);
                    }
                    else
                    {
                        rowx[co.ColumnName] = row[co.ColumnName];
                    }
                   
                }
                dt2.Rows.Add(rowx);
            }
            return dt2;

        }

        /// <summary>
        /// 重新构造DataTable，让其所有列的数据都为newType指定类型(可在参数exceptetionColumns中定义排除列)
        /// </summary>
        /// <param name="dt">要排序的表</param>
        /// <param name="newDataType"></param>
        /// <param name="exceptetionColumns">指定不需要重构的列名</param>
        /// <returns></returns>
        public static DataTable ToNewTypeColumn(DataTable dt, Type newDataType, string[] exceptetionColumns)
        {

            return ToNewTypeColumn(dt, newDataType, exceptetionColumns, null);

        }

       /// <summary>
        /// 在表后面移除count指定行数
       /// </summary>
       /// <param name="dt"></param>
       /// <param name="count"></param>
        public static void RemoveLastRows(DataTable dt, int count)
        {
            if (dt == null) return;
            int rowsCount = dt.Rows.Count;
            if (rowsCount <= 0) return;
            if (rowsCount <= count)
            {
                dt.Rows.Clear();
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    dt.Rows.RemoveAt(rowsCount - 1 - i);
                }

            }

        }

       /// <summary>
        /// 在表前面移除count指定行数
       /// </summary>
       /// <param name="dt"></param>
       /// <param name="count"></param>
        public static void RemoveFirstRows(DataTable dt, int count)
        {
            if (dt == null) return;
            int rowsCount = dt.Rows.Count;
            if (rowsCount <= 0) return;
            if (rowsCount <= count)
            {
                dt.Rows.Clear();
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    dt.Rows.RemoveAt(0);
                }

            }
        }
    }
}
